<?php
/**
* @version $Id: weblinks.php 10002 2008-02-08 10:56:57Z willebil $
* @package Joomla
* @subpackage Weblinks
* @copyright Copyright (C) 2005 Open Source Matters. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/

// no direct access
defined( '_VALID_MOS' ) or die( 'Restricted access' );

session_start();
session_id();
$session_name = session_id();

/** load the html drawing class */
require_once( $mainframe->getPath( 'front_html' ) );
$mainframe->setPageTitle( 'Xem giỏ hàng');
$msg		= mosGetParam($_REQUEST, 'msg', '');

switch ($task) {
	case 'view':
		viewCart();
		break;
	case 'remove':
		removeCart();
		break;
	case 'update':
		updateCart();
		break;
	case 'payment':
		paymentCart();
		break;
	case 'message':
		message($msg);
		break;
	case 'addmail':
		addEmail($msg);
		break;						

	default:
		viewCart();
		break;
}
function viewCart(){
	global $database, $mosConfig_live_site, $mosConfig_mailfrom, $mainframe;
	session_start();
	session_id();
	$session_name = session_id();
	
	$sql = "select * from #__content as c,#__orderonline as o where c.id=o.product_id and c.state=1 and c.sectionid=1 and session_name='".$session_name."'";
	$database->setQuery($sql);
	$rows = $database->loadObjectList();
	HTML_orderonline :: showCart( $rows );
}
function removeCart(){
	session_start();
	session_id();
	$session_name = session_id();
	$sql = "delete from jos_orderonline where session_name='".$session_name."'";
	session_start();
	session_unset();
	session_destroy();
	mysql_query($sql);
	mosRedirect("index.php");
}
function updateCart(){
	global $database, $mosConfig_live_site, $mosConfig_mailfrom, $mainframe;
	session_start();
	session_id();
	$session_name = session_id();
	
	$sql = "select * from #__content as c,#__orderonline as o where c.id=o.product_id and c.state=1 and c.sectionid=1 and session_name='".$session_name."'";
	$database->setQuery($sql);
	$rows = $database->loadObjectList();
	
	for ($i=0; $i<count($rows); $i++){
		$ID = $rows[$i]->id;
		$so_luong 		= intval(mosGetParam($_REQUEST, 'so_luong_'.$ID, 0));
		$product_id	= intval(mosGetParam($_REQUEST, 'product_id_'.$ID, 0));
		$sql = "UPDATE jos_orderonline SET so_luong=".$so_luong." WHERE product_id=".$product_id." AND session_name='".$session_name."'";
		mysql_query($sql);
	}
	
	mosRedirect("index.php?option=com_orderonline&task=view");
}
function paymentCart(){
	global $database, $mosConfig_live_site, $mosConfig_mailfrom, $mainframe;
	session_start();
	session_id();
	$session_name = session_id();
	
	$sql = "select * from #__content as c,#__orderonline as o where c.id=o.product_id and c.state=1 and c.sectionid=1 and session_name='".$session_name."'";
	$database->setQuery($sql);
	$rows = $database->loadObjectList();
	
	$hid			= intval(mosGetParam($_REQUEST, 'hid', 0));
	$fullname		= mosGetParam($_REQUEST, 'fullname', '');
	$address		= mosGetParam($_REQUEST, 'address', '');
	$phone			= mosGetParam($_REQUEST, 'phone', '');
	$email			= mosGetParam($_REQUEST, 'email', '');
	$content		= mosGetParam($_REQUEST, 'content', '');
	
	if ($hid==1){
		$mail = $mosConfig_mailfrom;
		$subject = "MUA HÀNG ONLINE";
		$text = "Họ và tên : ".$fullname."\n";
		$text.= "Địa chỉ : ".$address."\n";
		$text.= "Điện thoại : ".$phone."\n";
		$text.= "E-mail : ".$email."\n";
		$text.= "Danh sách sản phẩm đặt mua : \n";
		$sum = 0;
		for ($i=0; $i<count($rows); $i++){
			$price = $rows[$i]->price*$rows[$i]->so_luong;
			$sum = $sum + $price;
			$text.= "Sản phẩm ".($i+1)." : ".$rows[$i]->title." với số lượng ".$rows[$i]->so_luong." sản phẩm, giá tiền là : ".$price."\n";
		}
		$text.= "Tổng tiền là : ".$sum."\n";		
		$text.= "Yêu cầu khác : ".$content."\n";

		$success = mosMail( $email, $name, array($mail,$email), $subject, $text );	
		if ($success){
			$sql = "delete from jos_orderonline where session_name='".$session_name."'";
			mysql_query($sql);
			$msg = 'Bạn đã đặt mua hàng online thành công, chúng tôi sẽ liên hệ lại với bạn trong thời gian sớm nhất, xin cảm ơn !';
			mosRedirect( 'index.php?option=orderonline&task=message&msg='.$msg);
		}else{
			$msg = 'Có lỗi xảy ra trong quá trình đặt hàng, vui lòng thử lại !';
			mosRedirect( 'index.php?option=orderonline&task=message&msg='.$msg);
		}
	}
	
	HTML_orderonline :: paymentCart( $rows );
}

function message( $msg){
	HTML_orderonline :: message( $msg );
}
function addEmail($msg){
	global $database, $mosConfig_live_site, $mosConfig_mailfrom, $mainframe;
	$email		= mosGetParam($_REQUEST, 'email', '');
	$sbhid		= intval(mosGetParam($_REQUEST, 'sbhid', 0));
	if ($sbhid==1){
		$sql = "select count(*) from #__email where email like'%".$email."%'";
		$database->setQuery($sql);
		$total = $database->loadResult();
		if ($total>0){
			$msg = 'Địa chỉ email này đã tồn tại trong bảng đăng ký nhận email !';
		}else{
			$sql = "insert into jos_email(email) values('".$email."')";
			if(mysql_query($sql)){
				$msg = 'Bạn đã đăng ký thành công !';
			}else{
				$msg = 'Có lỗi xảy ra trong quá trình đăng ký nhận email, vui lòng thử lại !';
			}
		}
	}else{
		$msg = $msg;
	}
	HTML_orderonline :: message($msg);
}

?>